﻿namespace NJQuery {
	using System;
	using System.ComponentModel;
	using System.Web.UI;

	/// <summary>
	/// Specifies icons for the <see cref="Button"/> control.
	/// </summary>
	public class ButtonIcons : IClientHash {


		#region Static Readonly Fields: Property defaults

		private static readonly Icon defaultPrimary = Icon.None;
		private static readonly Icon defaultSecondary = Icon.None;

		#endregion


		#region Fields

		StateBag viewState;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ButtonIcons"/> class.
		/// </summary>
		/// <param name="viewState">The container control's <see cref="StateBag"/> (ViewState) object.</param>
		/// <exception cref="ArgumentNullException">View State cannot be null.</exception>
		public ButtonIcons(StateBag viewState) {
			if(viewState == null) throw new ArgumentNullException("viewState");

			this.viewState = viewState;
		}

		#endregion


		#region Properties: Public

		[DefaultValue(typeof(Icon), null)]
		public virtual Icon Primary {
			get { return viewState["Primary"] as Icon? ?? defaultPrimary; }
			set { viewState["Primary"] = value; }
		}

		[DefaultValue(typeof(Icon), null)]
		public virtual Icon Secondary {
			get { return viewState["Secondary"] as Icon? ?? defaultSecondary; }
			set { viewState["Secondary"] = value; }
		}

		/// <summary>
		/// Gets whether or not the defined properties are all set to default values.
		/// </summary>
		public bool IsDefault {
			get { return Primary == defaultPrimary && Secondary == defaultSecondary; }
		}

		#endregion


		#region Properties: Protected

		/// <summary>
		/// Gets a dictionary of state information that allows you to save and restore the view state of a server control across multiple requests for the same page.
		/// </summary>
		protected StateBag ViewState {
			get { return viewState; }
		}

		#endregion


		#region Methods: Rendering hooks

		/// <summary>
		/// Creates the properties object that will be rendered as the jQuery UI initialization options.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="properties">A <see cref="PropertyCollection"/> object that will become the jQuery UI initialization options.</param>
		protected virtual void AddInitProperties(PropertyCollection properties) {

			Icon primary = Primary;
			if(primary != defaultPrimary) properties.AddValue("primary", primary);

			Icon secondary = Secondary;
			if(secondary != defaultSecondary) properties.AddValue("secondary", secondary);

		}

		/// <summary>
		/// Returns a literal JavaScript value used to represent the server value on the client.
		/// </summary>
		/// <param name="indent">The client source indentation level.</param>
		/// <returns>The client script value.</returns>
		public string ToClientSource(int indent) {
			PropertyCollection childProperties = new PropertyCollection(indent);
			AddInitProperties(childProperties);
			return childProperties.ToClientSource();
		}

		#endregion


	}

}